{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Experiment\n",
    "folder = 'fig/lag4/DFM_ObsShock'\n",
    "\n",
    "# Import data\n",
    "data = np.genfromtxt(folder + '/tradeoff_best.csv', delimiter=',')\n",
    "h = data[0,1:].flatten()-1\n",
    "w = data[1:,0].flatten()\n",
    "z = data[1:,1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define colors and hatches\n",
    "\n",
    "methods = ['VAR', 'BC VAR', 'BVAR', 'LP', 'Pen LP', 'VAR avg'] # Method names\n",
    "lines = np.array([[0.4940,    0.1840,    0.5560],\n",
    "                  [0.9290,    0.6940,    0.1250],\n",
    "                  [0,         0.4470,    0.7410],\n",
    "                  [0.8500,    0.3250,    0.0980],\n",
    "                  [0.4660,    0.6740,    0.1880],\n",
    "                  [0.3010,    0.7450,    0.9330]]) # Colors for each method\n",
    "hatches = ['', '-', '.', '/', 'x', 'o'] # Hatch pattern for each method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create colormap\n",
    "\n",
    "numl = lines.shape[0]\n",
    "cmap = mpl.colors.ListedColormap(lines)\n",
    "norm = mpl.colors.BoundaryNorm(np.arange(0.5,numl+1), cmap.N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Figure\n",
    "\n",
    "mpl.rcParams.update({\"text.usetex\": True, \"font.family\": \"serif\"}) # Tex titles\n",
    "plt.rc('text', usetex=True)\n",
    "\n",
    "fig1, ax1 = plt.subplots()\n",
    "fig2, ax2 = plt.subplots()\n",
    "\n",
    "cs = ax1.pcolormesh(h, w, z, cmap=cmap, norm=norm) # Plot with colored regions\n",
    "\n",
    "for i in range(numl):\n",
    "    zm = np.ma.masked_not_equal(z, i+1)\n",
    "    ax1.pcolor(h, w, zm, hatch=hatches[i], alpha=0.) # Add hatch patterns one at a time\n",
    "\n",
    "cp = ax2.contourf(h, w, z, levels=np.arange(numl+1), cmap=cmap, norm=norm, hatches=hatches)\n",
    "# Hidden contour plot only used for the legend\n",
    "\n",
    "cbar = fig1.colorbar(cp, ticks=np.arange(numl)+0.5, aspect=10) # Legend\n",
    "cbar.ax.set_yticklabels(methods)\n",
    "cbar.ax.tick_params(size=0)\n",
    "\n",
    "plt.close(fig2) # Close the contour plot\n",
    "\n",
    "# Axis ticks+labels\n",
    "plt.xticks(np.arange(0,np.amax(h)+1,2))\n",
    "plt.xlabel('Horizon')\n",
    "plt.ylabel('Bias Weight')\n",
    "\n",
    "plt.savefig(folder + '/tradeoff_best.eps') # Save figure\n",
    "plt.savefig(folder + '/tradeoff_best.png', dpi=200)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
